package org.example.demo.service.sellerService.impl;

import org.example.demo.dto.sellerAccount.SellerAccountLoginParam;
import org.example.mbg.mapper.SellerAccountMapper;
import org.example.mbg.model.SellerAccount;
import org.example.mbg.model.SellerAccountExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.demo.service.sellerService.sellerAccountService;

import java.util.List;

@Service
public class sellerAccountServiceImpl implements sellerAccountService {

    @Autowired
    SellerAccountMapper sellerAccountMapper;

    @Override
    public Long register(String name, String password) {
        SellerAccount sellerAccount = new SellerAccount();
        sellerAccount.setName(name);
        sellerAccount.setPassword(password);
        sellerAccountMapper.insert(sellerAccount);
        return 0L;
    }

    @Override
    public SellerAccountLoginParam login(String name, String password) {
        SellerAccountExample sellerAccountExample = new SellerAccountExample();
        sellerAccountExample.createCriteria().andNameEqualTo(name);
        List<SellerAccount> sellerAccounts = sellerAccountMapper.selectByExample(sellerAccountExample);
        if (sellerAccounts.size() > 0) {
            SellerAccount sellerAccount = sellerAccounts.get(0);
            if (sellerAccount.getPassword().equals(password)) {
                SellerAccountLoginParam sellerAccountLoginParam = new SellerAccountLoginParam();
                sellerAccountLoginParam.setLoginName(name);
                sellerAccountLoginParam.setActualName(name);
                sellerAccountLoginParam.setNickName(name);
                sellerAccountLoginParam.setId(sellerAccount.getId());
                sellerAccountLoginParam.setPhone("1234656");
                return sellerAccountLoginParam;
            }
        }
        return null;
    }

    @Override
    public boolean exist(String name) {
        SellerAccountExample sellerAccountExample = new SellerAccountExample();
        sellerAccountExample.createCriteria().andNameEqualTo(name);
        return sellerAccountMapper.selectByExample(sellerAccountExample) != null;
    }


}
